﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Comm.Web.View
{
    public partial class Index : Basepage
    {
        protected string htmlCurrentPage = String.Empty;//当前页码
        protected string IndexSum = String.Empty;//共多少页
        protected string btnOrderBy = "价格↑";
        protected string btnOrderByDate = "出版日期↑";
        protected string bname = String.Empty;
        public override void SonLoad()
        {
            if (!IsPostBack)
            {

                BindRepeater(1);
            }
            if (!string.IsNullOrEmpty(Request.Form["htmlBtnPre"]))
            {
                int currentPage = Convert.ToInt32(Request.Form["currentPage"]);
                BindRepeater(--currentPage);
            }
            if (!string.IsNullOrEmpty(Request.Form["bName"]))
            {
                 bname = Request.Form["bName"];
                 ViewState["bName"] = bname;
                 BindRepeater(1);
            }
            if (!string.IsNullOrEmpty(Request.Form["htmlBtnNext"]))
            {
                int currentPage = Convert.ToInt32(Request.Form["currentPage"]);
                BindRepeater(++currentPage);
            }
            //按照价格排序
            if (!string.IsNullOrEmpty(Request.Form["btnPriceOrderby"]))
            {
                OrderByPrice(Request.Form["btnPriceOrderby"]);
            }
            else//改变排序按钮上的文字
            {
                if (ViewState["orderby"] != null)
                {
                    if (ViewState["orderby"].ToString() == "UnitPrice asc")
                    {
                        btnOrderBy = "价格↓";
                    }
                }
            }
            //按照出版日期排序
            if (!string.IsNullOrEmpty(Request.Form["btnGetDate"]))
            {
                OrderByDate(Request.Form["btnGetDate"]);
            }
            else
            {
                if (ViewState["orderbydate"] != null)
                {
                    if (ViewState["orderbydate"].ToString() == "PublishDate asc")
                    {
                        btnOrderByDate = "出版日期↓";
                    }
                }
            }
        }

        public void OrderByPrice(string txt)
        {
            if (txt == "价格↑")//升序
            {
                btnOrderBy = "价格↓";
                ViewState["orderby"] = "UnitPrice asc";
            }
            else//降序
            {
                btnOrderBy = "价格↑";
                ViewState["orderby"] = "UnitPrice desc";
            }
            ViewState["orderbydate"] = null;
            BindRepeater(1);//排序完成以后回到第一页。
        }

        public void OrderByDate(string txt)
        {
            if (txt == "出版日期↑")//升序
            {
                btnOrderByDate = "出版日期↓";
                ViewState["orderbydate"] = "PublishDate asc";
            }
            else//降序
            {
                btnOrderByDate = "出版日期↑";
                ViewState["orderbydate"] = "PublishDate desc";
            }
            ViewState["orderby"] = null;
            BindRepeater(1);//排序完成以后回到第一页。
        }
        protected void BindRepeater(int pageIndex)
        {
            int categoryId = 0;
            int pageCount = 0;
            //接收类别编号
            if (!int.TryParse(Request.QueryString["categoryId"], out categoryId))
            {
                categoryId = 0;
            }
            BLL.BooksService bll = new BLL.BooksService();
            if (ViewState["bName"] == null)
            {
                 pageCount = bll.GetPageCount(categoryId, 4);//求出指定类别下的总的页数
            }
            else
            {
                 bname = ViewState["bName"].ToString();
                 pageCount = bll.GetPageCount1(categoryId, 4, bname);//求出指定类别下的总的页数
            }
            if (pageIndex < 1)//判断当前页的范围
            {
                pageIndex = 1;
            }
            if (pageIndex > pageCount)
            {
                pageIndex = pageCount;
            }

            htmlCurrentPage = pageIndex.ToString();

            string orderby = string.Empty;
            if (ViewState["orderby"] != null)//获取了排序的方式
            {

                orderby = ViewState["orderby"].ToString();
            }
            if (ViewState["orderbydate"] != null)
            {
                orderby = ViewState["orderbydate"].ToString();
            }
            if (ViewState["bName"] != null)
            {
                bname = ViewState["bName"].ToString();
            }

            this.bookListRepeater.DataSource = bll.GetPageData(pageIndex, 4, categoryId, orderby,bname);
            this.bookListRepeater.DataBind();
            IndexSum = pageCount.ToString();

        }
        /// <summary>
        /// 对字符进行截取
        /// </summary>
        /// <returns></returns>
        protected string CutString(string str, int length)
        {
            if (str.Length > length)
            {
                return str.Substring(0, length) + "......";
            }
            else
            {
                return str;
            }

        }
        /// <summary>
        /// 确定静态文件所在的文件夹
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        protected string GetDir(object time)
        {
            DateTime d = Convert.ToDateTime(time);
            return "../StaticPage/" + d.Year + "/" + d.Month + "/" + d.Day + "/";
        }

        /// <summary>
        /// 格式化日期
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        protected string GetDate(object time)
        {
            DateTime d = Convert.ToDateTime(time);
            return d.ToShortDateString().ToString();
        }
    }
}